package weatherwidget.isrefluxer.bird.com.birdweatherwidget.until;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import weatherwidget.isrefluxer.bird.com.birdweatherwidget.R;
import weatherwidget.isrefluxer.bird.com.birdweatherwidget.WidgetApplication;
import weatherwidget.isrefluxer.bird.com.birdweatherwidget.constant.Lunar;

/**
 * 项目名称：BirdWeatherWidget
 * 类描述：
 * 创建人：Administrator
 * 创建时间：2016/11/19 15:06
 * 修改人：Administrator
 * 修改时间：2016/11/19 15:06
 * 修改备注：
 */
public class DateUtil {

    public static String MM_DD = "MM月dd日";
    public static String yyyy_MM_dd = "yyyy-MM-dd";
    public static String getYYYY_MM_DD(long ms) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(yyyy_MM_dd);
        return dateFormat.format(new Date(ms));
    }
    public static String getMM_DD(long ms){

        SimpleDateFormat dateFormat = new SimpleDateFormat(MM_DD);
        return dateFormat.format(new Date(ms));
    }

    public static String getWeek() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        switch (calendar.get(Calendar.DAY_OF_WEEK)){
            case Calendar.SUNDAY:
                return WidgetApplication.getInstance().
                        getResources().getString(R.string.sunday);
            case Calendar.MONDAY:
                return WidgetApplication.getInstance().
                        getResources().getString(R.string.monday);
            case Calendar.TUESDAY:
                return WidgetApplication.getInstance().
                        getResources().getString(R.string.tuesday);
            case Calendar.WEDNESDAY:
                return WidgetApplication.getInstance().
                        getResources().getString(R.string.wednesday);
            case Calendar.THURSDAY:
                return WidgetApplication.getInstance().
                        getResources().getString(R.string.thursday);
            case Calendar.FRIDAY:
                return WidgetApplication.getInstance().
                        getResources().getString(R.string.friday);
            case Calendar.SATURDAY:
                return WidgetApplication.getInstance().
                        getResources().getString(R.string.saturday);
            default:
                return WidgetApplication.getInstance().
                        getResources().getString(R.string.sunday);
        }

    }

    public static String getLunarDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getDefault());
        Lunar lunar = new Lunar();
        lunar.getLunar(calendar);
        return Lunar.getChinaDayString(lunar.day);
    }

    public static boolean isNight() {
        Calendar calendar = Calendar.getInstance();
        int hour_of_day = calendar.get(Calendar.HOUR_OF_DAY);
        if (hour_of_day >= 6 && hour_of_day < 18) {
            return false;
        } else {
            return true;
        }
    }

    public static long getTimeToNextDay() {
        Calendar calendar = Calendar.getInstance();
        int hour_of_day = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);

        return (23 - hour_of_day) * 60 * 60 * 1000 + (59 - minute) * 60 * 1000 + (59 - second)
                * 1000;
    }

    public static long getTimeToNight() {
        Calendar calendar = Calendar.getInstance();
        int hour_of_day = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);

        LogUtil.log("test1", "hour_of_day=" + hour_of_day);
        LogUtil.log("test1", "minute=" + minute);
        LogUtil.log("test1", "second=" + second);

        if (hour_of_day < 6) {
            return (5 - hour_of_day) * 60 * 60 * 1000 + (59 - minute) * 60 * 1000 + (59 - second)
                    * 1000;
        } else if (hour_of_day < 18) {
            return (17 - hour_of_day) * 60 * 60 * 1000 + (59 - minute) * 60 * 1000 + (59 - second)
                    * 1000;
        } else {
            return (23 - hour_of_day + 6) * 60 * 60 * 1000 + (59 - minute) * 60 * 1000
                    + (59 - second)
                    * 1000;
        }
    }
}
